package cn.gdcp.recycleviewtest;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.List;

public class MainActivityActivity extends AppCompatActivity implements IOnEditListener {
        Button addBtn;
        RecyclerView recyclerView;
       private List<Fruit> fruitList=new ArrayList<>();
    private List<Fruit> resultList=new ArrayList<>();
       FruitAdapter adapter;
       int position;
       EditText searchEdit;
       Button searchBtn;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        initfruit();
        initRecycleView();
        initAdpter();
        initButtonView();
        addBtnOnclick();
        initSearchView();
        initSearch();

    }

    private void initSearchView() {
      searchEdit=findViewById(R.id.search_edit);
      searchBtn=findViewById(R.id.search_button);
    }

    private void initSearch() {
         searchBtn.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 resultList.clear();
                 String content=searchEdit.getText().toString().trim();
                 Log.e("1",content);
                 for(int i=0;i<fruitList.size();i++){
                     Fruit fruit=fruitList.get(i);
                     if (fruit.getName().contains(content)){
                         resultList.add(fruit);
                         fruitList=resultList;
                            adapter.notifyDataSetChanged();
                     }
                 }
             }
         });

    }

    public void  edit(int position){
           Intent intent=new Intent(MainActivityActivity.this,EditActivity.class);
           startActivityForResult(intent,1001);
           this.position=position;
         }



    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
           if (requestCode==101&&resultCode==202){
             String name=data.getStringExtra("NAME");
             int age=Integer.parseInt(data.getStringExtra("AGE"));
             int imageId=data.getIntExtra("IMAGEID",0);
             Fruit fruit=new Fruit(name,imageId,age);
               fruitList.add(fruit);
               adapter.notifyDataSetChanged();
           }else  if (requestCode==1001&&resultCode==2002){
               String name=data.getStringExtra("NAME");
               int age=Integer.parseInt(data.getStringExtra("AGE"));
               int imageId=data.getIntExtra("IMAGEID",0);
               Fruit fruit=new Fruit(name,imageId,age);
                fruitList.set(position,fruit);
                adapter.notifyDataSetChanged();           }
    }
    private void addBtnOnclick() {
           addBtn.setOnClickListener(new View.OnClickListener() {
               public void onClick(View v) {
                   startActivityForAdd();
               }
           });

    }

    private void startActivityForAdd() {
            Intent intent=new Intent(MainActivityActivity.this,AddActivity.class);
                startActivityForResult(intent,101);
    }

    private void initButtonView() {
         addBtn=findViewById(R.id.add_button);
    }

    private void initAdpter() {
        adapter=new FruitAdapter(fruitList,MainActivityActivity.this,MainActivityActivity.this);
        recyclerView.setAdapter(adapter);
    }

    private void initRecycleView() {
      recyclerView=(RecyclerView)findViewById(R.id.recyclerView);
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
    }

    private void initfruit(){
            Fruit a=new Fruit("A",R.drawable.icon,11);
            fruitList.add(a);
            Fruit b=new Fruit("B",R.drawable.icon,12);
            fruitList.add(b);
            Fruit c=new Fruit("C",R.drawable.icon,13);
            fruitList.add(c);
            Fruit d=new Fruit("D",R.drawable.icon,14);
            fruitList.add(d);
            Fruit e=new Fruit("E",R.drawable.icon,15);
            fruitList.add(e);
            Fruit f=new Fruit("F",R.drawable.icon,16);
            fruitList.add(f);
            Fruit g=new Fruit("G",R.drawable.icon,17);
            fruitList.add(g);

    }


}
